home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_01 / pugh / aspeed.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-30  |  1.5 KB  |  64 lines

  1. Listing 1
  2. **************************************************
  3. /*  aspeed.c   array vs pointer speed test  */
  4. #include <stdio.h>
  5. #include <time.h>   /* gettime() */
  6.  
  7. #define  SIZE 1000
  8. #define  REPS 1000
  9.  
  10. void zerobyincrement(double array[], int howmany);
  11. void zerobydecrement(double array[], int howmany);
  12. void zerobypointer(double array[], int howmany);
  13. double fetchtime( void);
  14.  
  15. void main()
  16.     {
  17.     double darray[SIZE];
  18.     int i;
  19.     double t0, t1;
  20.     printf("incrementing index...\n");
  21.     t0 = fetchtime();
  22.     for (i=0; i<REPS; i++)
  23.       zerobyincrement(darray, SIZE);
  24.     t1 = fetchtime();
  25.     printf("time... %9.2lf \n", (t1-t0) / CLOCKS_PER_SEC);
  26.     printf("decrementing index...\n");
  27.     t0 = fetchtime();
  28.     for (i=0; i<REPS; i++)
  29.       zerobydecrement(darray, SIZE);
  30.     t1 = fetchtime();
  31.     printf("time... %9.2lf \n", (t1-t0) / CLOCKS_PER_SEC);
  32.     printf("marching pointer... \n");
  33.     t0 = fetchtime();
  34.     for (i=0; i<REPS; i++)
  35.       zerobypointer(darray, SIZE);
  36.     t1 = fetchtime();
  37.     printf("time... %9.2lf \n", (t1-t0) / CLOCKS_PER_SEC);
  38.     }
  39.  
  40. void zerobyincrement(double array[], int howmany)
  41.     {
  42.     int i;
  43.     for (i=0; i<howmany; i++)
  44.       array[i] = 0.0;
  45.     }
  46.  
  47. void zerobydecrement(double array[], int howmany)
  48.     {
  49.     int i;
  50.     for (i=howmany-1; i>=0; i--)
  51.       array[i] = 0.0;
  52.     }
  53.  
  54. void zerobypointer(double array[], int howmany)
  55.     {
  56.     while (howmany--)
  57.       *array++ = 0.0;
  58.     }
  59.  
  60. double fetchtime( void )
  61.     {
  62.     return (double) clock();
  63.     }
  64.